﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Firefly.Forms;

namespace Firefly.Controls {
    public class FormManager : ApplicationContext {

        public Dictionary<string, Form> Forms { get; private set; }

        public List<Form> FormStack { get { return mFormStack; } }

        public FormManager() {
            Forms = new Dictionary<string, Form>();
            mFormStack = new List<Form>();
            // Initialize!!!
            InitializeForms();
            // Register events...
            RegisterEvents();
            // This is a first run, at runtime, so open the World Manager and the Client.
            OpenForm("Client");
            OpenForm("World Selector");
            //Data.WorldFile.Save(new World("Starfall", "Wolfsinger.com", 3763));
        }

        public void OpenForm(string s) {
            if (Forms.ContainsKey(s)) {
                Forms[s].Show();
                MakeTopMost(Forms[s]);
            } else
                throw new Exception("No such form exists by the name of " + s);
        }

        private void InitializeForms() {
            Forms.Add("Client", new ClientForm());
            Forms.Add("World Selector", new WorldSelectorForm());
            Forms.Add("World Editor", new WorldEditorForm());
            Forms.Add("Client Settings", new ClientSettingsForm());
            Forms.Add("Grid Editor", new GridEditorForm());
        }

        private void RegisterEvents() {
            foreach (Form f in Forms.Values) {
                f.FormClosing += new FormClosingEventHandler(f_FormClosing);
            }
            // Clientform specific events.
            ClientForm c = (ClientForm)Forms["Client"];
            // Activity handler
            c.OnActivity += new EventHandler(delegate(object s, EventArgs e) {
                c.Flash();
            });
        }

        private void f_FormClosing(object sender, FormClosingEventArgs e) {
            Form f = (Form)sender;
            mFormStack.RemoveAt(GetFormStackIndex(f));
            MakeTopMost(mFormStack[mFormStack.Count - 1]);
            if(mFormStack.Count - 1 != -1 )
                MakeTopMost(mFormStack[mFormStack.Count - 1]);
        }

        private void MakeTopMost(Form form) {
            for (int i = 0; i < mFormStack.Count; i++) {
                if (form != mFormStack[i]) {
                    mFormStack[i].TopMost = false;
                    mFormStack[i].Enabled = false;
                }
                else {
                    mFormStack.RemoveAt(i);
                }
            }
            mFormStack.Add(form);
            //form.TopMost = true;
            form.BringToFront();
            form.Enabled = true;
        }

        private int GetFormStackIndex(Form form) {
            int index = 0;
            foreach (Form f in mFormStack)
                if (f == form)
                    return index;
                else
                    index++;
            // Found nothing.
            return -1;
        }

        private List<Form> mFormStack;
    }
}
